package math.probability;

import java.util.ArrayList;
import java.util.Random;

public class CompDistribution extends ProbDistribution {
	private Double dlta;
	private ProbDistribution d1;
	private ProbDistribution d2;
	
	public CompDistribution(Double dlta, ProbDistribution d1, ProbDistribution d2) {
		this.dlta = dlta;	
		this.d1 = d1;
		this.d2 = d2;
	}
	
	
	
	public ArrayList<Double> getSample(int n) {
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(getNext());
		}
		return sample;
	}
	
	public ArrayList<Double> getSample(int n, Random r) {
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(getNext(r));
		}
		return sample;
	}
	
	public String toString() {
		return String.format("%1.2f", 1-dlta) + d1.toString() + " + " + String.format("%1.2f", dlta) + d2.toString();
	}



	@Override
	public Double getNext() {
		Random r = new Random();
		Double alpha = r.nextDouble();
		if (alpha<(1-dlta))
			return d1.getNext();
		else 
			return d2.getNext();
	
	}
	
	public Double getNext(Random r) {
		Double alpha = r.nextDouble();
		if (alpha<(1-dlta))
			return d1.getNext();
		else 
			return d2.getNext();
	
	}
	
	public Double getDisp() {
		return Math.pow(1-dlta, 2.0)*d1.getDisp() + Math.pow(dlta, 2.0)*d2.getDisp();
	}
}
